package kr.co.hk.depart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import kr.co.hk.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.widget.Toast;

public class LotteDetail extends Activity {
	private WebView webView;
	final Activity activity = this;
	private String urls;
	private String html = "";

	private ProgressDialog progressDialog;
	final private int PROGRESS_DIALOG = 0;
	
	private boolean pageEx = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotte_detail);

		Bundle extra = getIntent().getExtras();
		urls = extra.getString("url");

		webView = (WebView) findViewById(R.id.lotteWebView);
		webView.clearCache(true);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setBuiltInZoomControls(true);
		
		mainProcessing();
	}

	private Handler chdHandler = new Handler();

	private void mainProcessing() {
		pageEx = false;
		showDialog(PROGRESS_DIALOG);
		Thread childThread = new Thread(null, dobackgroundThreadProcessing,
				"Backgroud");
		childThread.start();
	}

	private Runnable dobackgroundThreadProcessing = new Runnable() {

		@Override
		public void run() {
			backgroudnThreadProcessing();
		}
	};

	private void backgroudnThreadProcessing() {
		getHtml(urls);
		chdHandler.post(doUpdateGUI);
	}

	private Runnable doUpdateGUI = new Runnable() {

		@Override
		public void run() {
			updateGUI();
		}
	};

	// gui code
	private void updateGUI() {
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		progressDialog.dismiss();
		LotteDetail.this.removeDialog(PROGRESS_DIALOG);
		
		if (pageEx) {
			excePro();
		} else {
		
			webView.loadDataWithBaseURL(urls, html, "text/html", "utf-8", null);
		}
		
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:

			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("Now loading..");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(false);
			return progressDialog;

		}
		return null;

	}
	
	public void getHtml(String addr) {
		StringBuilder body = new StringBuilder();
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setReadTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream(),
									"euc-kr"));
					String line = "";

					boolean bodyChk = false;

					while ((line = br.readLine()) != null) {

						if (urls.contains("shopping_news_read")) {

							if (line.contains("<!-- BBS READ -->")) {

								bodyChk = true;
							}

							if (line.contains("</html>")) {
								bodyChk = false;
								break;
							}
						} else {
							if (line.contains("<!-- Template_Top -->")) {

								bodyChk = true;
							}

							if (line.contains("<!-- BBS LIST -->")) {

								bodyChk = false;
								break;
							}
						}

						if (bodyChk) {
							body.append(line);
						}

					}
					String css = "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.lotteshopping.com/common/css/common.css\">";
					html = css + body.toString();
					br.close();
				}
				conn.disconnect();
			}
		} catch (Exception ex) {
			pageEx= true;
			ex.printStackTrace();
		}

	}
	private void excePro() {
		toast("페이지 로딩시 오류가  있습니다. \n 네트워크 확인 후 다시 시도 하세요!");
		onBackPressed();
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
